package View;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import Control.CtrlLogin;
import Model.Funcionario;
import Model.ItfOuvinte;

import javax.swing.border.TitledBorder;

public class Tlogin extends JDialog implements ItfOuvinte{

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtEmail;
	private JPasswordField txtSenha;
	private JButton btnEntrar;
	private JButton btnFechar;

	public Tlogin() {
		setUndecorated(true);
		setModal(true);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 299, 139);
		getContentPane().setLayout(new BorderLayout());
		setLocationRelativeTo(null);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Acesso ao Sistema", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 280, 90);
		contentPanel.add(panel);
		panel.setLayout(null);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(48, 28, 223, 20);
		panel.add(txtEmail);
		txtEmail.setColumns(10);
		
		JLabel lblEmail = new JLabel("E-mail");
		lblEmail.setBounds(10, 31, 46, 14);
		panel.add(lblEmail);
		
		JLabel lblSenha = new JLabel("Senha");
		lblSenha.setBounds(10, 58, 36, 14);
		panel.add(lblSenha);
		
		txtSenha = new JPasswordField();
		txtSenha.setBounds(48, 56, 91, 20);
		panel.add(txtSenha);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				btnEntrar = new JButton("Entrar");
				btnEntrar.setActionCommand("Entrar");
				buttonPane.add(btnEntrar);
				getRootPane().setDefaultButton(btnEntrar);
			}
			{
				btnFechar = new JButton("Sair");
				btnFechar.setActionCommand("Sair");
				buttonPane.add(btnFechar);
			}
		}
		
	}

	@Override
	public void registerListener(Object t) {
		CtrlLogin control = (CtrlLogin) t;
		btnEntrar.addActionListener(control);
		btnFechar.addActionListener(control);
	}

	@Override
	public void populaCampos(Object t) {
	}

	@Override
	public Object populaObjeto(Object t) {
		Funcionario func = (Funcionario) t;
		func.setLogin(txtEmail.getText());
		func.setSenha(new String(txtSenha.getPassword()));
		return func;
	}

	@Override
	public void liberaCampos(boolean libera) {
	}

	@Override
	public void limpaCampos() {
		txtEmail.setText(null);
		txtSenha.setText(null);
	}

	@Override
	public boolean validaCampos() {
		if(txtEmail.getText().length()==0 || (new String(txtSenha.getPassword()).length())==0){
			return true;
		}
		return false;
	}
}
